package com.y.demo.controller;

import com.y.demo.entity.AddOneUser;
import com.y.demo.entity.User;
import com.y.demo.entity.UpdateUser;
import com.y.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@ResponseBody
public class UserController {
	@Autowired(required=false)
	private UserMapper userMapper;

	//查看所有操作用户
	@RequestMapping("/userSelect")
	public List<User> findAll(){
		return userMapper.findAll();
	}
	//查看一个用户
	@RequestMapping("/userSelectOne")
	public User findOne(@RequestParam("id") int id){
		return userMapper.findOne(id);
	}
	//更改
	@RequestMapping("/userUpdate")
	public int updateUser(@RequestBody UpdateUser user){
		System.out.println(user);
		return userMapper.updateUser(user.getUpdateUserName(),user.getUpdateUserPhone(),user.getUpdateId());
	}
	//删除
	@RequestMapping("/userDelete")
	public int deleteUser(@RequestParam("deleteId") int id){
		return userMapper.deleteUser(id);
	}
	//新增
	@RequestMapping("/userInsert")
	public int insertUser(@RequestBody AddOneUser user){
		if (user.getUserName() == null){
			return 0;
		}else return userMapper.insertUser(user.getUserName(),user.getUserPhone());
	}

}
